﻿using MrPP.iot.Guide;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
   
    public class CellHighlightManager : MonoBehaviour
    {

        [SerializeField]
        private BoxModel _model;


        [SerializeField]
        private BoxManager _manager;

        public void Start() {

            _model.onLight += doLight;
        }
        private void OnDestory()
        {
            _model.onLight -= doLight;
        }
        GuideTarget _target = null;
        private void doLight(CabinetGroup.ItemData oldValue, CabinetGroup.ItemData newValue)
        {
            var oldCell = _manager.getCell(oldValue.location);
            var newCell = _manager.getCell(newValue.location);
            if (oldCell != null) {
                down(oldCell);
            }
            if (newCell != null)
            {
                up(newCell, newValue);
            }
            else {

               
            }

           // oldValue.location
        }
        private Cell _curl = null;
        private void up(Cell cell, CabinetGroup.ItemData data)
        {
            CellDraw draw = cell.GetComponent<CellDraw>();
            draw.close();
            draw.show(true, data.location.type);




            GuideManager.Instance.target = draw.target;
            draw.setHighlight(data.text, data.location);
            _curl = cell;
        }

        private void down(Cell cell)
        {  

            CellDraw draw = cell.GetComponent<CellDraw>();
            draw.close();

            GuideManager.Instance.target = null;
        }
        public void close() {
            if (_curl != null) {
                down(_curl);
            }
        }
        public void open() { 
        
        }
        /*

public BoxManager manager
{
get
{
return _manager;
}
}

Dictionary<string, ItemData> table_ = new Dictionary<string, ItemData>();
internal void setup(CabinetGroup.ItemData[] datas)
{

foreach (var item in table_)
{
var cell = _manager.getCell(item.Value.location);
if (cell)
{
  cell.check.all = new List<ItemData>();
}
}
table_.Clear();
Dictionary<BoxLocation, List<ItemData>> map = new Dictionary<BoxLocation, List<ItemData>>();
foreach (var data in datas)
{
if (!map.ContainsKey(data.location))
{
  map.Add(data.location, new List<ItemData>());
}
map[data.location].Add(data);

table_.Add(data.sn, data);
}

foreach (var item in map)
{
Cell cell = _manager.getCell(item.Key);
if (cell != null)
{
  cell.check.all = item.Value;
}

}
}

private List<string> _last = new List<string>();
internal void check(List<string> list)
{

List<string> other = list.Except(_last).ToList();
foreach (string sn in other)
{
ItemData data = this.getData(sn);
if (!string.IsNullOrEmpty(data.sn))
{
  Cell cell = _manager.getCell(data.location);
  Debug.LogError(data.sn);
  cell.check.addChecked(data);
  _last.Add(data.sn);
}
}


}

private ItemData getData(string sn)
{
if (table_.ContainsKey(sn))
{
return table_[sn];
}
return new ItemData();
}
*/


    }
}